<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{ path: '/' }">
      <i class="el-icon-s-promotion"></i>后台管理
    </el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/news/newsDetail' }">新闻管理</el-breadcrumb-item>
    <el-breadcrumb-item><a href="/">新闻详情</a></el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>
  <div>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="新闻题目" prop="title">
      <el-input v-model="ruleForm.title" readonly></el-input>
    </el-form-item>
    <el-form-item label="作者" prop="author">
      <el-input v-model="ruleForm.author" readonly></el-input>
    </el-form-item>
    <el-form-item label="资讯分类" prop="newsCategoryId">
      <el-input v-model="ruleForm.newsCategoryName" readonly></el-input>
    </el-form-item>
<!--    <el-input label="正文"-->
<!--        type="content"-->
<!--        :rows="12"-->
<!--        placeholder="请输入内容"-->
<!--        v-model="ruleForm.content">-->
<!--    </el-input>-->
<!--    <el-form-item label="正文" prop="content">-->
<!--      <el-input v-model="ruleForm.content"></el-input>-->
<!--    </el-form-item>-->

        <el-form-item label="正文" prop="content">
              <el-input
                  type="textarea"
                  :rows="12"
                  placeholder="请输入内容"
                  v-model="ruleForm.content"
                  readonly>
              </el-input>
        </el-form-item>
    <el-form-item>
      <el-button type="warning" @click="$router.push('/news/newsDetail')">返回列表</el-button>
    </el-form-item>
  </el-form>

  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        id:'',
        title: '',
        author:'',
        newsCategoryId:'',
        newsCategoryName: '',
        content:'',

      },
      rules: {
        title: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        author: [
          {required: true, message: '请输入作者', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        newsCategoryId: [
          {required: true, message: '请选择资讯分类', trigger: 'blur'},
        ],
        content: [
          {required: true, message: '请输入正文内容', trigger: 'blur'},
        ]
      },
      // 富文本编辑器
      editor: {},
      newsCategoryList: []
    };
  },
  methods: {
    // 初始化富文本编辑器
    initWangEditor() {
      this.editor = new this.wangEditor('#wangEditor');
      this.editor.config.zIndex = 1;
      this.editor.create();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9200/newsDetail/add-new';
          this.ruleForm.content = this.editor.txt.html();

          let formData = this.qs.stringify(this.ruleForm);

          this.axios.post(url,formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state==20000){
              this.$message({
                message: '添加新闻成功',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadNewsDetailStandard() {
      let id = this.$router.currentRoute.query.id;
      let url = 'http://localhost:9200/newsDetail/standard?id=' + id;
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.ruleForm = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    loadNewsCategoryList() {
      let url = 'http://localhost:9200/news-category/list';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.newsCategoryList = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted(){
    this.loadNewsDetailStandard();
    this.initWangEditor();
    this.loadNewsCategoryList();
  }
}
</script>

<style scoped>

</style>